package cn.strategy.fund;

import cn.stock.util.MyUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取哪些板块的资金是持续流入的
 * */
public class Inflow_type {
    public static List<List<Object>> inflow() {
        List<List<Object>> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT name, zhangdie, main_fund, main_fund_bf FROM type_fund");
            rs = stmt.executeQuery();
            while (rs.next()) {
                List<Object> r = new ArrayList<>();
                List<Double> f = getFund_5(rs.getString(1));
//                if (rs.getString(1).equals("贵金属")) {
//                    System.out.println("吴大富");
//                }
                if (!f.isEmpty()) {
                    r.add(rs.getString(1));
                    Double zhangdie = rs.getDouble(2);
                    Double main_fund = rs.getDouble(3);
                    Double main_fund_bf = rs.getDouble(4);
                    r.add(zhangdie - f.get(0));
                    r.add(main_fund - f.get(1));
                    r.add(main_fund_bf - f.get(2));
                    // 即使流出的速度加快，也要是正流出的
                    r.add(main_fund);
                    lst.add(r);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
//        return lst.stream().filter(a -> (double) a.get(4) > 0D).sorted((a, b) -> {
//            if ((double)a.get(2) - (double)b.get(2) > 0)
//                return -1;
//            return 1;
//        }).collect(Collectors.toList());
        return lst.stream().sorted((a, b) -> {
            if ((double)a.get(2) - (double)b.get(2) > 0)
                return -1;
            return 1;
        }).collect(Collectors.toList());
    }

    public static List<Double> getFund_5(String type_name) {
        List<Double> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT zhangdie, main_fund, main_fund_bf FROM type_fund_5 where name = '"+ type_name +"'");
            rs = stmt.executeQuery();
            while (rs.next()) {
                lst.add(rs.getDouble(1));
                lst.add(rs.getDouble(2));
                lst.add(rs.getDouble(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lst;
    }
}

